/**
 * @author [siwi]
 * @email [siwi@siwi.me]
 * @create date 2017-09-16 11:43:19
 * @modify date 2017-09-16 11:43:19
 * @desc [管理员表 不公开]
*/

const { mongoose, Schame } = require('../Mongoose')
const crypto = require('crypto')

const Table = new Schame({
    display_name: {
        type: String,
        default: 'User',
    },
    username: {
        type: String,
        unique: true,
        required: true,
        index: true
    },
    email: {
        type: String,
        unique: true,
        required: true,
        index: true
    },
    password: {
        type: String,
        required: true,
        index: true
    },
    remember_token: {
        type: String,
    },
    avatar: {
        type: String,
        default: null
    }
})

/**
 * 密码写入加密
 */

Table.path('password').set(password => crypto.createHash('md5').update(password).digest('hex'))

/**
 * 虚拟字段 头像
 */
Table.virtual('avatar_img').get(() => 'https://semantic-ui.com/images/avatar2/large/matthew.png')

/**
 * 检测密码
 */

Table.statics.check_password = async (username, password) => {
    let admin = await Admin.findOne({
        username: username,
        password: crypto.createHash('md5').update(password).digest('hex')
    })
    return admin
}

const Admin = mongoose.model('Admin', Table)
module.exports = Admin